﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;

namespace WebSite.admin.product
{
    public partial class goods_add07 : System.Web.UI.Page
    {


        // Methods
        private void AddItemFromSourceListBox(ListBox sourceBox, ListBox targetBox, string aa)
        {
            string str = "0";
            foreach (ListItem item in targetBox.Items)
            {
                if (sourceBox.SelectedValue.ToString() == item.Value.ToString())
                {
                    str = "1";
                    break;
                }
            }
            if (aa.ToString() == "1")
            {
                foreach (ListItem item2 in sourceBox.Items)
                {
                    if ((item2.Selected && !targetBox.Items.Contains(item2)) && (str.ToString() == "0"))
                    {
                        targetBox.Items.Add(new ListItem(item2.Text, "" + sourceBox.SelectedValue.ToString() + ""));
                    }
                }
            }
            else
            {
                foreach (ListItem item3 in sourceBox.Items)
                {
                    if (!targetBox.Items.Contains(item3) && (str.ToString() == "0"))
                    {
                        targetBox.Items.Add(new ListItem(item3.Text, "" + item3.Value + ""));
                    }
                }
            }
        }

        protected void btnToAllLeft_Click(object sender, EventArgs e)
        {
            foreach (ListItem item in this.lbRight.Items)
            {
                SQLAccess.DoSql("delete from DB_Pronew where newsid=" + item.Value + " and  proid=" + base.Request.QueryString["pid"].ToString() + "");
            }
            this.RemoveSelectedItemAll(this.lbRight);
        }

        protected void btnToAllRight_Click(object sender, EventArgs e)
        {
            string aa = "";
            if (this.lbLeft.Items.Count > 0)
            {
                this.AddItemFromSourceListBox(this.lbLeft, this.lbRight, aa);
            }
        }

        protected void btnToLeft_Click(object sender, EventArgs e)
        {
            if (this.lbRight.SelectedItem != null)
            {
                SQLAccess.DoSql("delete from DB_Pronew where newsid=" + this.lbRight.SelectedValue.ToString() + " and  proid=" + base.Request.QueryString["pid"].ToString() + "");
                this.RemoveSelectedItem(this.lbRight);
            }
        }

        protected void btnToRight_Click(object sender, EventArgs e)
        {
            string aa = "1";
            if (this.lbLeft.SelectedItem != null)
            {
                this.AddItemFromSourceListBox(this.lbLeft, this.lbRight, aa);
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (base.Request.QueryString["pid"] != null)
            {
                for (int i = 0; i < this.lbRight.Items.Count; i++)
                {
                    string text = this.lbRight.Items[i].Text;
                    string sql = "Select * From DB_Pronew Where newsid =" + this.lbRight.Items[i].Value.ToString() + " and proid=" + base.Request.QueryString["pid"].ToString() + "";
                    DataTable table = new DataTable();
                    table = SQLAccess.GreatDs(sql).Tables[0];
                    if (table.Rows.Count > 0)
                    {
                        SQLAccess.DoSql("delete from DB_Pronew where pronewid=" + table.Rows[0]["pronewid"].ToString() + "");
                    }
                    SQLAccess.DoSql("insert into DB_Pronew (proid,newsid) values (" + base.Request.QueryString["pid"].ToString() + "," + this.lbRight.Items[i].Value.ToString() + ")");
                }
                base.Response.Write("<script language='javascript'>window.location.href='goods_add07.aspx?pid=" + base.Request.QueryString["pid"] + "';</script>");
                base.Response.End();
            }
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            string str = "0";
            string sql = "select * from DB_News where visible=1 and languages=" + info.languages("") + " ";
            if (re.replace(this.keyname.Value.ToString()) != "")
            {
                sql = sql + " and newsname like '%" + re.replace(this.keyname.Value.ToString()) + "%' ";
            }
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    for (int i = 0; i < this.lbLeft.Items.Count; i++)
                    {
                        if (row["newsid"].ToString() == this.lbLeft.Items[i].Value.ToString())
                        {
                            str = "1";
                            break;
                        }
                    }
                    if (str.ToString() == "0")
                    {
                        this.lbLeft.Items.Add(new ListItem(row["newsname"].ToString(), row["newsid"].ToString()));
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session["Adminlogin"] == null)
            {
                base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
                base.Response.End();
            }
            this.ImageButton1.Attributes["onclick"] = "return clicka();";
            if (!base.IsPostBack && (base.Request.QueryString["pid"] != null))
            {
                this.upbind();
            }
        }

        private void RemoveSelectedItem(ListBox listControl)
        {
            while (listControl.SelectedIndex != -1)
            {
                listControl.Items.RemoveAt(listControl.SelectedIndex);
            }
        }

        private void RemoveSelectedItemAll(ListBox listControl)
        {
            if (listControl.Items.Count > 0)
            {
                listControl.Items.Clear();
            }
        }

        private void upbind()
        {
            string sql = "select * from DB_Pronew where proid=" + base.Request.QueryString["pid"];
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    string str2 = "select top 1 * from DB_News where newsid=" + row["newsid"].ToString();
                    DataTable table2 = new DataTable();
                    table2 = SQLAccess.GreatDs(str2).Tables[0];
                    if (table2.Rows.Count > 0)
                    {
                        this.lbRight.Items.Add(new ListItem(table2.Rows[0]["newsname"].ToString() + "", row["newsid"].ToString()));
                    }
                }
            }
        }

     
    }
}